package diary

import (
	"context"
	"time"

	"go-weixin/app/diary/api/internal/svc"
	"go-weixin/app/diary/api/internal/types"
	"go-weixin/app/diary/rpc/pb/proto/diary"

	"github.com/zeromicro/go-zero/core/logx"
)

type DiaryAddLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewDiaryAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DiaryAddLogic {
	return &DiaryAddLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *DiaryAddLogic) DiaryAdd(req *types.DiaryAddReq) (resp *types.DiaryAddResp, err error) {
	ctx, canelFunc := context.WithTimeout(context.Background(), 5*time.Second)
	defer canelFunc()
	_, err = l.svcCtx.DiaryRpc.DiaryAdd(ctx, &diary.DiaryAddReq{
		Title:   req.Title,
		Content: req.Content,
		UserId:  req.UserId,
	})
	if err != nil {
		return nil, err
	}
	return
}
